#ifndef HTTP_SERVER_H
#define HTTP_SERVER_H

#include "host_list.h"
#include "event.h"
#include "write_file_range.h"

typedef struct {
	host_list_t host_list;

	int have_proxy;
	const char *proxy_logfile;
	int proxy_log_fd;

	int port; /* Port to bind to. */
	int listener; /* Listener socket. */
	int event_manager; /* epoll file descriptor / kqueue file descriptor. */

	int running;

	int max_file_descriptors;
	unsigned nfds; /* # of file descriptors. */

	int *index;
	event_t *events;

	http_connection_t *http_connections;

	http_connection_t **interrupted_http_connections;
	size_t number_interrupted_http_connections;

	time_t current_time;
	struct tm stm;
	struct tm local_time;

	int handle_alarm;
	int handle_sigusr1;
	int handle_sighup;

	time_t max_idle_time;

	int dirlisting;
	int have_authentication;

	int log_requests;

	const char *user;

	const char *chroot_directory;

	const char *logfile;
	int log_fd;
	buffer_t logbuffer;

	create_event_manager fn_create_event_manager;
	add_fd fn_add_fd;
	del_fd fn_del_fd;
	del_listener fn_del_listener;
	event_wait fn_event_wait;
	prepare_for_reading fn_prepare_for_reading;
	prepare_for_writing fn_prepare_for_writing;
	remove_notifications fn_remove_notifications;

	write_file_range fn_write_file_range;
} http_server_t;

int http_server_load_configuration (http_server_t *http_server, configuration_t *conf);
int http_server_create (http_server_t *http_server);
void http_server_delete (http_server_t *http_server);

void http_server_copy (http_server_t *dest, http_server_t *src);

int start_http_server (http_server_t *http_server);
void stop_http_server (http_server_t *http_server);

void http_server_remove_connection (http_server_t *http_server, int client);
int http_server_create_backend_connection (http_server_t *http_server, int client, int fd);
int http_server_create_fcgi_connection (http_server_t *http_server, int client, int fd);

extern http_server_t http_server;

#endif /* HTTP_SERVER_H */
